package com.zeny.express.framework.scheduler.quartz;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import com.zeny.express.framework.scheduler.entity.SchedulerInfoEntity;
import com.zeny.express.framework.scheduler.service.SchedulerLogService;


public class CentralizedSchedulerFactoryBean extends SchedulerFactoryBean {
	
	private List<JobDetail> jobs;
	
	@Autowired
	private SchedulerLogService schedulerLogService;
	
	@Override
	protected void registerJobsAndTriggers() throws SchedulerException {
		
		// set triggers
		List<Trigger> triggers = new ArrayList<Trigger>();
		
		schedulerLogService.storeSchedulerInfos(this.getScheduler().getSchedulerName());
		
		for (JobDetail job : jobs) {
			
			JobDetailImpl jobDetail = (JobDetailImpl)job;
			
			Collection<SchedulerInfoEntity> infos = schedulerLogService
					.querySchedulerInfos(jobDetail.getName());
			
			for (SchedulerInfoEntity info : infos) {

				JobDetailFactoryBean jobDetailBean = new JobDetailFactoryBean();
				jobDetailBean.setName(info.getJobId());
				jobDetailBean.setGroup(jobDetail.getName());
				jobDetailBean.setJobClass(job.getJobClass());
				jobDetailBean.setDurability(true);
				jobDetailBean.getJobDataMap().put(BaseJob.DATA_THREAD_COUNT, info.getThreadCount());
				jobDetailBean.getJobDataMap().put(BaseJob.DATA_THREAD_NO, info.getThreadNo());
				jobDetailBean.getJobDataMap().put(BaseJob.DATA_PARAM, info.getParam());
				jobDetailBean.afterPropertiesSet();
				
				if (BaseJob.JOB_TYPE_SIMPLE.equalsIgnoreCase(info.getJobType()) ) {
					SimpleTriggerFactoryBean triggerBean = new SimpleTriggerFactoryBean();
					triggerBean.setName(info.getJobId());
					triggerBean.setJobDetail(jobDetailBean.getObject());
					triggerBean.setRepeatInterval(info.getSimpleRepeatIntervel() * 1000l);
					try {
						triggerBean.afterPropertiesSet();
					} catch (ParseException e) { }
					
					triggers.add(triggerBean.getObject());
					
				} else if (BaseJob.JOB_TYPE_CRON.equalsIgnoreCase(info.getJobType()) ) {
					CronTriggerFactoryBean triggerBean = new CronTriggerFactoryBean();
					triggerBean.setName(info.getJobId());
					triggerBean.setJobDetail(jobDetailBean.getObject());
					triggerBean.setCronExpression(info.getCronExpression());
					triggerBean.afterPropertiesSet();
					
					triggers.add(triggerBean.getObject());
				}
			}
		}
		
		this.setTriggers(triggers.toArray(new Trigger[0]));
		
		// super execution
		super.registerJobsAndTriggers();
	}

	
	/**
	 * @param jobs
	 */
	public void setJobs(List<JobDetail> jobs) {
		this.jobs = jobs;
	}


}
